package com.vertx.core;

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.WorkerExecutor;

/**
 * @version v1.0
 * @Description: 创建Vertx实例对象
 * @Author: TongRui乀
 * @Date: 2021/5/2 12:16 下午
 */
public class NewVertx {

    public static void newVertx_01(){

        Vertx vertx = Vertx.vertx();

    }

    public static void newVertx_02(){

        VertxOptions options = new VertxOptions();

        options.setWorkerPoolSize(20);
        options.setInternalBlockingPoolSize(2);
        options.setMaxEventLoopExecuteTime(1000);

        Vertx vertx = Vertx.vertx(options);

    }

    public static void newVertx_03(){

        VertxOptions options = new VertxOptions();


        Vertx.clusteredVertx(options, event -> {
            if (event.succeeded()) {
                System.out.println("Vertx cluster create success....");
            }
            if (event.failed()) {
                System.out.println("Vertx cluster create failure...." + event.cause());
            }
        });

    }


    public static void executeBlocking(){

        Vertx vertx = Vertx.vertx();

        vertx.executeBlocking(promise->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("execute block");
            promise.complete("result block");
        }, res->{
            System.out.println(res.result());
        });

    }

    public static void executeBlockingWithWorker(){

        WorkerExecutor executor = Vertx.vertx().createSharedWorkerExecutor("my-worker-pool");
        executor.executeBlocking(future -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            future.complete("success");
        }, res -> {
            System.out.println("The result is: " + res.result());
        });

    }


    public static void main(String[] args) {

        executeBlockingWithWorker();

    }

}
